package com.dy.basic.controller;

import com.dy.basic.util.AjaxResult;
import com.dy.basic.util.BusinessException;
import com.dy.user.dto.PhoneRegisterFormDto;
import com.dy.user.dto.WechatDto;
import com.dy.user.service.IWxuserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private IWxuserService wxuserService;

    /**
     * callback.html 发送微信请求
     * @param wechatDto
     * @return
     */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody @Valid WechatDto wechatDto){
        try {
            return wxuserService.wechat(wechatDto);
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail("网络异常");
        }
    }

    /**
     * binder.html 注册界面
     * @param phoneRegisterFormDto
     * @return
     */
    @PostMapping("/wechat/binder")
    public AjaxResult wechatBinder(@RequestBody @Valid PhoneRegisterFormDto phoneRegisterFormDto){
        try {
            return wxuserService.wechatBinder(phoneRegisterFormDto);
        }catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail("网络异常");
        }
    }
}
